<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>用户</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../layui/css/layui.css"  media="all">
    <link rel="stylesheet" href="../../static/css/style.css"  media="all">
    <script type="text/javascript" src="../../static/js/jquery.min.js" ></script>
    <script src="../../layui/layui.js"></script>
    <script src="../../static/js/base.js" charset="utf-8"></script>
    <script src="../../static/js/ax.js"></script>
    <script src="../../static/js/jquery.cookie.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body class="bg_w">
<div class="layui-fluid">
    <div class="content-wrap">
        <form class="layui-form layui-form-pane" id="from1">
            <input type="hidden" name="id" id="id" />
            <div class="layui-row layui-col-space15">
                <div class="layui-col-sm6">
                    <label class="layui-form-label">用户账号<span class="red">*</span></label>
                    <div class="layui-input-block">
                        <input type="text" name="username" id="username" readonly  class="layui-input" lay-verify="required" placeholder="用户账号字母+数字，50字符内">
                    </div>
                </div>
                <div class="layui-col-sm6">
                    <label class="layui-form-label">用户姓名<span class="red">*</span></label>
                    <div class="layui-input-block">
                        <input type="text" name="name" id="name"  class="layui-input"  lay-verify="required|name" placeholder="用户姓名只能输入汉字、字母和数字，100字符内">
                    </div>
                </div>
            </div>
            <div class="layui-row layui-col-space15">
                <div class=" layui-col-md6">
                    <label class="layui-form-label">角色：</label>
                    <div class="layui-input-block">
                        <select id="roleId" name="roleId">
                            <option value="">选择</option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="layui-row layui-col-space15">
                <div class="layui-col-xs6">
                    <label class="layui-form-label">管理部门<span class="red">*</span></label>
                    <div class="layui-input-block">
                        <input type="hidden" name="depCode"  id="depCode"/>
                        <input type="text" disabled="disabled" name="depName" id="depName" class="layui-input"  lay-verify="required" placeholder="请选择部门">
                    </div>
                </div>
                <div class="layui-col-xs6">
                    <button type="button" onclick="dep()" class="layui-btn layui-btn-primary">部门</button>
                </div>
            </div>
            <div class="layui-row layui-col-space15">
                <div class="layui-col-xs6">
                    <label class="layui-form-label" >管理岗位<span class="red">*</span></label>
                    <div class="layui-input-block">
                        <input type="hidden" name="positionId"  id="positionId"/>
                        <input type="text" disabled="disabled" name="positionName" id="positionName" class="layui-input"   lay-verify="required" placeholder="请选择岗位">
                    </div>
                </div>
                <div class="layui-col-xs6">
                    <button type="button" onclick="position()" class="layui-btn layui-btn-primary">岗位</button>
                </div>
            </div>


            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn add_btn btn-left" lay-submit="" lay-filter="addNews">提交</button>
                    <!-- <button type="reset" class="layui-btn layui-btn-primary">重置</button>-->
                </div>
            </div>
        </form>
    </div>
</div>

<script>
    var roleId;
    function getRole() {
        ax.post({
            url: backendWebUrl + '/api/department/queryByOrganizationCode',
            contentType: "application/x-www-form-urlencoded",
            data: '',
            success: function (r) {
                $('#roleId').empty();
                $("#roleId").append("<option value=''>选择</option>");
                $(r.data).each(function (i, n) {
                    $("#roleId").append("<option value='"+n.id+"'>" + n.roleName + "</option>");
                    if(roleId==n.id){
                        $("#roleId").val(n.id);
                    }
                });
                form.render('select');
            },
        });
    }
    window.onload=function(){
        var  id=decodeURI(getQueryString('id'));
        ax.post({
            type: "post",
            url: backendWebUrl+"/user/getUserId",
            data: {id:id},
            contentType: "application/x-www-form-urlencoded",
            success: function (data) {
                $("#id").val(data.data.id);
                $("#depCode").val(data.data.depCode);
                $("#depName").val(data.data.depName);
                $("#positionId").val(data.data.positionId);
                $("#positionName").val(data.data.positionName);
                $("#name").val(data.data.name);
                $("#username").val(data.data.username);
                roleId=data.data.roleId;
            }
        });
        getRole();
    }

    function dep() {
        layui.layer.open({
            id:'',
            title : "部门组织",
            type : 2,
            area: ['600px', '400px'],
            btn: ['确定','取消'],
            content : "departmentList.html",
            yes: function (index,layero) {
                var departmentCode = layero.find("iframe")[0].contentWindow.$("#departmentCode").val();
                var departmentName = layero.find("iframe")[0].contentWindow.$("#departmentName").val();
                if(departmentCode==null || departmentCode==""){
                    layer.msg("请选择部门");
                }else{
                    $("#depCode").val(departmentCode);
                    $("#depName").val(departmentName);
                    layer.close(index);
                }
            } ,
            btn2: function (index,layero) {
                layer.close(index);
            }
        });
    }

    function position() {
        var departmentCode=$("#depCode").val();
        if(departmentCode==null || departmentCode==""){
            layer.msg("请先选择部门");
            return false;
        }
        layui.layer.open({
            id:'',
            title : "岗位列表",
            type : 2,
            area: ['850px', '350px'],
            btn: ['确定','取消'],
            content : "positionList.html?departmentCode="+$("#depCode").val(),
            yes: function (index,layero) {
                var positionId = layero.find("iframe")[0].contentWindow.$("#positionId").val();
                var positionName = layero.find("iframe")[0].contentWindow.$("#positionName").val();
                if(positionId==null || positionId==""){
                    layer.msg("请选择岗位");
                }else{
                    $("#positionId").val(positionId);
                    $("#positionName").val(positionName);
                    layer.close(index);
                }
            } ,
            btn2: function (index,layero) {
                layer.close(index);
            }
        });
    }
    var form;
    layui.config({
        base: "js/"
    }).use(['form', 'layer', 'jquery', 'layedit', 'laydate', 'upload'], function (){
        form = layui.form;
        var layer = parent.layer === undefined ? layui.layer : parent.layer;
        var laypage = layui.laypage;
        var layedit = layui.layedit;
        var laydate = layui.laydate;
        var $ = layui.jquery;

        //添加验证规则
        form.verify({
            username:[
                /^[a-zA-Z][a-zA-Z0-9]{1,50}$/,
                "用户名必须是字母+数字，50字符内！"
            ],
            name:[
                /^[a-zA-Z0-9\u4e00-\u9fa5]{1,100}$/,
                '用户姓名只能输入汉字、字母和数字，100字符内'
            ],
        });

        form.on("submit(addNews)", function (data) {
            ax.post({
                type: "post",
                url: backendWebUrl+"/user/updateUser",
                data: $("#from1").serialize(),
                contentType: "application/x-www-form-urlencoded",
                success: function (data) {
                    layer.msg(data.message);
                }
            });
            return false;
        });
    })
</script>
</body>
</html>